package com.ujoku.controller.admin;

import com.labillusion.core.platform.cdn.upyum.UpYun;
import com.labillusion.core.platform.web.rest.RESTController;
import com.ujoku.domain.CMSContent;
import com.ujoku.service.CMSContentService;
import com.ujoku.service.GoodsService;
import com.ujoku.view.builder.ActivityViewBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

/**
 * Created by Andy.Lian on 2015/3/7.
 */
@Controller
public class ActivityMngController extends RESTController {
    @Autowired
    protected CMSContentService cmsContentService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ActivityViewBuilder activityViewBuilder;
    @Autowired
    private UpYun upyun;

    @Value("#{configProperties['settings.activity.upload']}")
    private String uploadPath;

    @Value("#{configProperties['settings.cdn.activity.upload']}")
    private String cdnUploadPath;

    @RequestMapping(value="/uj-admin/activity/save",method = RequestMethod.POST)
    @ResponseBody
    public String saveActivity(@Valid @RequestBody CMSContent activityContent){
        CMSContent content=cmsContentService.selectActivtyContent();
        if(content!=null){
            content.setContent(activityContent.getContent());
            cmsContentService.update(content);
            return "Successful";
        }
        content=new CMSContent();
        content.setContent(activityContent.getContent());
        cmsContentService.insert(content);
        return "Successful";
    }

    @RequestMapping(value = "/uj-admin/activity/get",method = RequestMethod.GET)
    @ResponseBody
    public CMSContent get(){
        CMSContent content=cmsContentService.selectActivtyContent();
        if(content!=null){
            return content;
        }
        return null;
    }

    @RequestMapping(value="/uj-admin/activity/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam MultipartFile file, @RequestParam int index, HttpServletRequest request) throws IOException {

        String dir = request.getRealPath(uploadPath);
        File folder = new File(dir);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String originalPath = dir+"/act"+ index +".jpg";
        File originalFile = new File(originalPath);
        //将上传文件写入到指定文件出
        file.transferTo(originalFile);

        File  localFile = new File(originalPath);
        upyun.setContentMD5(UpYun.md5(localFile));
        upyun.writeFile(cdnUploadPath + localFile.getName(), localFile, true);

        CMSContent content=cmsContentService.selectActivtyContent();
        cmsContentService.update(content);

        return "Successfully";
    }
}
